package com.xybean.action.base;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xybean.dao.page.Page;

/**
 * @author RJW {@rennuoting@126.com}
 * @version 1.0
 * @copyright (C), 2007-2008, RJW. This program is protected by copyright laws.
 * @file AbstractAction.java
 * @date 2008-11-3 下午07:34:13
 * @desc AbstractAction封装了Action的一些常见的操作,�yi一般自己的Action可以继承该Action
 */
@SuppressWarnings("serial")
public class AbstractAction extends ActionSupport {

	private static Logger looger = LoggerFactory.getLogger(AbstractAction.class);
	
	protected int defaultPageSize = 5;
	protected Integer id;
	protected Integer currentPage = 1;
	protected Long totalPageCount = 0l;
	protected String returnmsg;

	// 导航条,默认为主页
	protected Integer navNumber = 1;

	protected Page page = new Page();

	/**
	 * 设置导航条
	 */
	protected void setNavNumber(Integer num) {
		navNumber = num;
	}
	/**
	 * 字符串转成double
	 */
	protected Double toDouble(String num) {
		return Double.parseDouble(num);
	}

	/**
	 * 手机号码验证：13 15 18
	 */
	protected boolean validateMoblie(String phone) {
		int l = phone.length();
		boolean rs=false;
		switch (l) {
		case 7:
			if (matchingText("^(13[0-9]|15[0-9]|18[7|8|9|6|5])\\d{4}$", phone)) {
				rs= true;
			}
			break;
		case 11:
			if (matchingText("^(13[0-9]|15[0-9]|18[7|8|9|6|5])\\d{4,8}$", phone)) {
				rs= true;
			}
			break;
		default:
			rs=false;
		break;
		}
		return rs;
	}
	protected boolean validatePostcode(String postcode) {
		int l = postcode.length();
		boolean rs=false;
		if(l==6&&matchingText("^\\d{6}$", postcode)){
			rs= true;
		}
		return rs;
	}
	protected boolean validateqq(String qq) {
		int l = qq.length();
		boolean rs=false;
		if(l>=6&&l<=12&&matchingText("^\\d{6,12}$", qq))
		{
			rs= true;
		}
		return rs;
	}
	private static boolean matchingText(String expression, String text) {
		Pattern p = Pattern.compile(expression); // 正则表达式
		Matcher m = p.matcher(text); // 操作的字符串
		boolean b = m.matches();
		return b;
	}

	/**
	 * Get an object from the Struts2 user request
	 */
	protected Object getFromRequest(String name) {
		return ServletActionContext.getRequest().getAttribute(name);
	}

	protected Object getFromApp(String name) {
		return ServletActionContext.getServletContext().getAttribute(name);
	}

	protected String getRequestParameter(String name) {
		return ServletActionContext.getRequest().getParameter(name);
	}

	protected void addCookie(String name, String value) {
		Cookie cookie = new Cookie(name, value);

		ServletActionContext.getResponse().addCookie(cookie);
	}
	protected void addCookie(String name, String value,Integer expiry) {
		Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(expiry);
		ServletActionContext.getResponse().addCookie(cookie);
	}

	protected String getCookieValueByName(String name) {
		Cookie[] cookies = ServletActionContext.getRequest().getCookies();
		Cookie cookie = null;
		for (int i = 0; i < cookies.length; i++) {
			cookie = cookies[i];
			if (cookie.getName().equals(name)) {
				return cookie.getValue();
			}
		}
		return null;
	}

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected String getRealPath(String path) {
		return ServletActionContext.getServletContext().getRealPath(path);
	}

	protected String getRequestIP() {
		return ServletActionContext.getRequest().getRemoteAddr();
	}

	protected String getHeadMethod() {
		return ServletActionContext.getRequest().getMethod().toLowerCase();
	}

	protected void setToRequest(String name, Object value) {
		ServletActionContext.getRequest().setAttribute(name, value);
	}

	protected void setToApplication(String name, Object value) {
		ServletActionContext.getServletContext().setAttribute(name, value);
	}

	protected Object getFromSession(String name) {
		return ActionContext.getContext().getSession().get(name);
	}

	protected void setToSession(String name, Object value) {
		ActionContext.getContext().getSession().put(name, value);
	}

	protected void cleanSession(String key) {
		ActionContext.getContext().getSession().remove(key);
	}

	protected void cleanSession() {
		ActionContext.getContext().getSession().clear();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getReturnmsg() {
		return returnmsg;
	}

	public void setReturnmsg(String returnmsg) {
		this.returnmsg = returnmsg;
	}

	public Long getTotalPageCount() {
		return totalPageCount;
	}

	public int getDefaultPageSize() {
		return defaultPageSize;
	}

	public void setDefaultPageSize(int defaultPageSize) {
		this.defaultPageSize = defaultPageSize;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public void setTotalPageCount(Long totalPageCount) {
		this.totalPageCount = totalPageCount;
	}

	public Integer getNavNumber() {
		String requestURI = getRequest().getRequestURI();
		looger.debug("访问的路径是" + requestURI);
		return NavNumberPovider.getnavNumber(requestURI);
	}
}
